package sword_offer;

/**
 * @author Synhard
 * @version 1.0
 * @class Code56
 * @description 剑指 Offer 53 - I. 在排序数组中查找数字 I
 * 统计一个数字在排序数组中出现的次数。
 *
 *  
 *
 * 示例 1:
 *
 * 输入: nums = [5,7,7,8,8,10], target = 8
 * 输出: 2
 * 示例 2:
 *
 * 输入: nums = [5,7,7,8,8,10], target = 6
 * 输出: 0
 *  
 *
 * 限制：
 *
 * 0 <= 数组长度 <= 50000
 *
 *
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-07-14 7:39
 */
public class Code56 {
    public static void main(String[] args) {

    }

    public int search(int[] nums, int target) {
        if (nums.length == 0) {
            return 0;
        }
        int low = -1, high = nums.length, mid, tgtNum = 0;
        while (low + 1 != high) {
            mid = (low + high) >> 1;
            if (nums[mid] < target) {
                low = mid;
            } else {
                high = mid;
            }
        }
        while (high < nums.length && nums[high] == target) {
            tgtNum++;
            high++;
        }
        return tgtNum;
    }
}
/*
二分查找到目标值后继续向后遍历
 */